package com.scommunity.uniapp.controller;

import com.scommunity.common.ResponseResult;
import com.scommunity.domain.House;
import com.scommunity.domain.Owner;
import com.scommunity.domain.User;
import com.scommunity.service.IHouseService;
import com.scommunity.service.IUserService;
import com.scommunity.service.ShortMessageService;
import com.scommunity.utils.JWTUtils;
import com.scommunity.utils.RandomUtils;
import com.scommunity.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UUserController {
    @Autowired
    private ShortMessageService shortMessageService;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private IUserService userService;

    @Autowired
    private IHouseService houseService;

    @Autowired
    private JWTUtils jwtUtils;


    @GetMapping("/login/{phone}")
    public ResponseResult sendMessage(@PathVariable String phone) {

        String code = RandomUtils.buildCheckCode(4);
        boolean flag = shortMessageService.sendSms(phone, code);
        if (flag) {
            redisUtils.set("loginCode:" + phone, code, 600);
            return ResponseResult.success();
        }
        return ResponseResult.error(500, "发送验证码错误");
    }

    @GetMapping("/loginByPhone")
    public ResponseResult loginByPhone(String phone, String verifyCode, HttpServletRequest request) {
        User u = userService.loginByPhone(phone);
        String code = redisUtils.get("loginCode:" + phone, String.class);
        if (u == null) {
            return ResponseResult.error(201, "手机号错误");
        } else {
            if ("".equals(verifyCode)) {
                return ResponseResult.error(202, "验证码为空");
            } else {
                if (code == null) {
                    return ResponseResult.error(203, "验证码已过期");
                } else {
                    if (!code.equals(verifyCode)) {
                        return ResponseResult.error(204, "验证码错误");
                    } else {
                        if (u.getUserStatus().equals("0")) {
                            return ResponseResult.error(205, "账号异常");
                        }
//                        request.getSession().setAttribute("user", u);
//                        return ResponseResult.success();
                        String token = jwtUtils.getToken(u);
                        return ResponseResult.success(token);
                    }
                }
            }
        }
    }

    @GetMapping("/loginByAccount")
    public ResponseResult loginByAccount(String account, String password, HttpServletRequest request) {
        User u = userService.loginByAccount(account, password);
        if (u == null) {
            return ResponseResult.error(201, "账号或密码错误");
        } else {
            if (u.getUserStatus().equals("0")) {
                return ResponseResult.error(202, "账号异常");
            }
//            request.getSession().setAttribute("user", u);
//            return ResponseResult.success();
            String token = jwtUtils.getToken(u);
            return ResponseResult.success(token);
        }
    }

    @GetMapping("/getInfoByUserId")
    public Object getInfoByUserId(@SessionAttribute User user) {
        Owner info = userService.getInfoByUserId(user.getUserId());
        return ResponseResult.success(info);
    }

    @GetMapping("/getHouseByUserId")
    public Object getHouseByUserId(@SessionAttribute User user) {
        Long ownerId = userService.getOwnerId(user.getUserId());
        List<House> houses = houseService.getOwnerHouses(ownerId.toString());
        for (House house : houses) {
            System.out.println(house);
        }
        return ResponseResult.success(houses);
    }

}
